# !/usr/bin/python
# -*- coding:utf-8 -*-
import sys, os, time
import ConfigParser
class Config:
    def __init__(self, path):
        self.path = path
        self.cf = ConfigParser.ConfigParser()
        self.cf.read(self.path)
    #获取字符串
    def get_str(self, field, key):
        result = ""
        try:
            result = self.cf.get(field, key)
        except:
            result = ""
        return result
    #获取int
    def get_int(self, field, key):
        result = 0
        try:
            result = self.cf.getint(field, key)
        except:
            result = 0
        return result
    #获取boolean
    def get_bool(self, field, key):
        result = False
        try:
            result = self.cf.getboolean(field, key)
        except:
            result = False
        return result

    #获取dict
    def get_dict(self,field):
        result={}
        try:
            kvs=self.cf.items(field)
            for key,value in kvs:
                result[key]=value
        except Exception as e:
            result={}
        return result
    #获取list
    def get_list(self,field,key):
        result = []
        try:
            result = self.cf.get(field, key).split(',')
        except:
            result = []
        return result


    def set(self, filed, key, value):
        try:
            self.cf.set(filed, key, value)
            self.cf.write(open(self.path, 'w'))
        except:
            return False
        return True
if __name__=="__main__":
    config=Config('./my.cfg')
    dict=config.get_dict('beanstalk_conf')
    print dict

